package com.zsq.thread.test3;

/**
 * @author : zhang sq
 * @date : 2019/12/25 22:19
 **/
public class Account {
    /**
     * set  bataice的值还没有set进去便开始读取bataice  bataice=0.00 (脏读) 给get方法加synchronized可解决
     */
    String name;
    double bataice;

    public synchronized void set(String name,double bataice){
        this.name=name;
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.bataice=bataice;
    }

    public /*synchronized */ double getBataice(String name){
        return this.bataice;
    }

    public static void main(String[] args) {
        Account accou=new Account();
        new Thread(()-> accou.set("zhangsan",100.0)).start();
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(accou.getBataice("zhangsan"));
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(accou.getBataice("zhangsan"));
    }

}
